<?php
namespace app\api\model;
use app\admin\model\TermEmergencyLang;
use think\Model;
use think\model\concern\SoftDelete;
class Nursing extends Model
{
    use SoftDelete;
    protected $name = "nursing";
    protected $pk = "id";
    protected $deleteTime = "delete_time";
    protected $defaultSoftDelete = 0;
    protected $json = [];
    protected $jsonAssoc = true;
    public function matching($data){
        if(empty($data)){
            return false;
        }
        $newdata = [];
        foreach ($data as $key=>$value){
            $value['name'] = NursingType::where('id',$value['id'])
                ->value('name');
            $ids = Nursing::where('status',1)
                ->where('name','in',$value['option'])
                ->where('type',$value['id'])
                ->where('weight','>',0)
                ->column('id');
//            $allids = array_merge($allids,$ids);
            $value['ids'] = $ids;
            $newdata[$value['id']] = $value;
        }
        $nursing_lang = NursingLang::where('status',1)
            ->where('type_id',4)
            ->field('id,name')
            ->order('sort asc')
            ->select()->toArray();
//        echo '<pre>';
//        print_r($nursing_lang);
//        exit;
        $nursing = null;
        foreach ($nursing_lang as $k=>$v){
//            echo '<pre>';
//            print_r($ids);
//            exit;
            if($v['name']=='特需照护'){
                if(!empty($newdata[9]['ids'])){
                    $ids = TermEmergencyLang::where('status',1)
                        ->where('nursing_lang_id',$v['id'])
                        ->where('nursing_type_id',$newdata[9]['id'])
                        ->column('nursing_id');
                    $shenyu = array_intersect($ids,$newdata[9]['ids']);
                    if(!empty($shenyu)){
                        $nursing = $v;
                        break;
                    }
                }
            }elseif ($v['name']=='术后护理'){
                if(!empty($newdata[4]['ids'])){
                    $ids = TermEmergencyLang::where('status',1)
                        ->where('nursing_lang_id',$v['id'])
                        ->where('nursing_type_id',$newdata[4]['id'])
                        ->column('nursing_id');
                    $shenyu = array_intersect($ids,$newdata[4]['ids']);
                    if(!empty($shenyu)){
                        $nursing = $v;
                        break;
                    }
                }

            }elseif ($v['name']=='康复护理'){
                if(!empty($newdata[9]['ids'])){
                    $ids = TermEmergencyLang::where('status',1)
                        ->where('nursing_lang_id',$v['id'])
                        ->where('nursing_type_id',$newdata[9]['id'])
                        ->column('nursing_id');
                    $shenyu = array_intersect($ids,$newdata[9]['ids']);
                    if(!empty($shenyu)){
                        $nursing = $v;
                        break;
                    }
                }

            }elseif ($v['name']=='认知行为照护'){
                if(!empty($newdata[7]['ids'])){
                    $ids = TermEmergencyLang::where('status',1)
                        ->where('nursing_lang_id',$v['id'])
                        ->where('nursing_type_id',$newdata[7]['id'])
                        ->column('nursing_id');
                    $shenyu = array_intersect($ids,$newdata[7]['ids']);
                    if(!empty($shenyu)){
                        $nursing = $v;
                        break;
                    }
                }
            }else{
                $nursing = $v;
                break;
            }
        }
        return $nursing;
    }
}